﻿using System;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

using LTJ.Entity;
using LTJ.Common;
using LTJ.Config;
using LTJ.FrameWork;

namespace LTJ.WebPage.SystemManage.route
{
    /// <summary>
    /// 整柜规则设置
    /// </summary>
    public partial class fclrulegrid : AdminPage
    {
        protected string parmStr = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataGrid();
            }
        }

        protected void BindDataGrid()
        {
            string strWhere = "";

            if (ViewState["condition"] == null || ViewState["condition"].ToString() == "")
            {
                if (parmStr != "") strWhere = parmStr;
            }
            else
            {
                if (parmStr != "") strWhere = ViewState["condition"].ToString() + " AND " + parmStr;
                else strWhere = ViewState["condition"].ToString();
            }

            DataGrid1.VirtualItemCount = Rules.GetRulesCount(strWhere);
            DataGrid1.DataSource = Rules.GetRulesByPage(DataGrid1.CurrentPageIndex + 1, DataGrid1.PageSize, strWhere, "rtype asc,id", "desc");
            DataGrid1.DataBind();
        }

        protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            DataGrid1.CurrentPageIndex = e.NewPageIndex;
            BindDataGrid();
        }

        protected void GoToPagerButton_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 搜索点击
        /// </summary>
        protected void check_Click(object sender, EventArgs e)
        {
            ViewState["condition"] = Rules.GetSysRuleSearchCondition(Utils.StrToInt(ruletype.SelectedValue, 0), SASRequest.GetString("ajaxcarrierlist_value"), SASRequest.GetString("ajaxroutelinedatalist_value"), destport.Text, SASRequest.GetString("username"));
            DataGrid1.CurrentPageIndex = 0;
            BindDataGrid();
        }

        /// <summary>
        /// 重新绑定
        /// </summary>
        protected void reclick_Click(object sender, EventArgs e)
        {
            BindDataGrid();
        }

        /// <summary>
        /// 删除规则
        /// </summary>
        protected void del_Click(object sender, EventArgs e)
        {
            if (SASRequest.GetString("cid") != "")
            {
                Rules.DeleteRulesByIds(SASRequest.GetString("cid"));
                BindDataGrid();
            }
        }

        /// <summary>
        /// 获取全局名称
        /// </summary>
        /// <param name="objDR"></param>
        /// <param name="type">1,名称；2，为航线</param>
        /// <returns></returns>
        protected string GetObjName(System.Data.DataRow objDR, int type)
        {
            string s_rvalue = "全局通用设置";
            int s_rtype = TypeConverter.ObjectToInt(objDR["rtype"], 0);
            int s_rid = TypeConverter.ObjectToInt(objDR["rid"], 0);
            int s_oid = TypeConverter.ObjectToInt(objDR["oid"], 0);
            int s_uid = TypeConverter.ObjectToInt(objDR["uid"], 0);


            if (s_oid == -1)
            {
                if (type == 1) s_rvalue = "全局线路设置";
                else s_rvalue = objDR["seaname1"].ToString();
            }
            else if (s_oid > 0)
            {
                if (type == 1)
                {
                    s_rvalue = "公司全局";
                    if (s_rtype == 1) s_rvalue = "公司航线";
                    if (s_rtype == 2) s_rvalue = "公司港口";
                }
                else s_rvalue = objDR["seaname"].ToString() + " " + objDR["boatname"].ToString();
            }

            return s_rvalue;
        }

        #region Web 窗体设计器生成的代码

        override protected void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }

        private void InitializeComponent()
        {
            parmStr = "uid = -1";
            DataGrid1.PageIndexChanged += new DataGridPageChangedEventHandler(DataGrid1_PageIndexChanged);
            DataGrid1.GoToPagerButton.Click += new EventHandler(GoToPagerButton_Click);
            DataGrid1.AllowCustomPaging = true;
            DataGrid1.TableHeaderName = "规则列表";
            DataGrid1.Attributes.Add("borderStyle", "0");
            DataGrid1.DataKeyField = "id";
            DataGrid1.ColumnSpan = 12;
            DataGrid1.SaveDSViewState = true;

            //check.Click += new EventHandler(check_Click);
            reclick.ServerClick += new EventHandler(reclick_Click);
            del.Click += new EventHandler(del_Click);
        }

        #endregion
    }
}